home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Format 1994 October
/
Macformat17.cdr
/
Shareware City
/
Developers
/
shutdown-fx-201-c
/
sfx ƒ
/
sfx control app ƒ
/
Shell ƒ
/
help.c
< prev
next >
Wrap
Text File
|
1994-07-11
|
13KB
|
532 lines
/**********************************************************************\
File: help.c
Purpose: This module handles displaying the different help windows.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program in a file named "GNU General Public License".
If not, write to the Free Software Foundation, 675 Mass Ave,
Cambridge, MA 02139, USA.
\**********************************************************************/
#include "help.h"
#include "environment.h"
#include "util.h"
#include "buttons.h"
#include "timing.h"
#include "program globals.h"
#define DEAD_SPACE_TOP 10
#define DEAD_SPACE_LEFT 10
#define DEAD_SPACE_BOTTOM 27
#define DEAD_SPACE_RIGHT 10
#define TEXT_RECT_WIDTH 405
#define TEXT_RECT_HEIGHT 250
#define BUTTON_WIDTH 62
#define BUTTON_HEIGHT 17
#define BUTTON_GAP 1
#define XREF_DEAD_SPACE_TOP 5
#define XREF_TEXT_WIDTH 46
#define XREF_WIDTH 70
#define XREF_HEIGHT 17
#define XREF_GAP 5
#define MAX_MAIN_TOPICS 20
#define MAX_XREFS 4
#define MAIN_TOPIC_ID 600
#define theWindowWidth (boundsRect.right-boundsRect.left)
#define theWindowHeight (boundsRect.bottom-boundsRect.top)
#define CorrectTime 1
typedef unsigned char **CharHandle;
typedef struct
{
long offset;
short lineHeight;
short fontDescent;
short fontNum;
unsigned char fontStyle;
unsigned char unused1;
short fontSize;
short unused2;
short unused3;
short unused4;
} OneStyle;
typedef struct
{
short numStyles;
OneStyle theStyle[31];
} StylRec, *StylPtr, **StylHandle;
enum
{
kLeft=0,
kCenter
};
short gNumMainTopics;
short gNumXRefs[MAX_MAIN_TOPICS];
Str31 gMainTopicTitle[MAX_MAIN_TOPICS];
Rect gMainTopicRect[MAX_MAIN_TOPICS];
short gMainTopicID[MAX_MAIN_TOPICS];
short gXRefIndex[MAX_MAIN_TOPICS][MAX_XREFS];
Rect gXRefRect[MAX_XREFS];
short gMainTopicShowing; /* saved in prefs file */
Rect gTextRect;
CharHandle gTheText;
StylHandle gTheStyle;
/*-----------------------------------------------------------------------------------*/
/* internal stuff for help.c */
static void SetupTheHelpWindow(WindowDataHandle theData);
static void ShutdownTheHelpWindow(WindowDataHandle theData);
static void InitializeTheHelpWindow(WindowDataHandle theData);
static void OpenTheHelpWindow(WindowDataHandle theData);
static void KeyPressedInHelpWindow(WindowDataHandle theData, unsigned char keyPressed);
static void MouseClickedInHelpWindow(WindowDataHandle theData, Point mouseLoc);
static void DrawTheHelpWindow(short theDepth);
static void DrawTheText(CharHandle theText, StylHandle theStyleHandle, short theJust,
short theMode, Rect theRect);
static void DrawTheShadowBox(Rect theRect);
static short ParseRawTitle(Str255 theTitle, short *xRef, short *numXRefs);
static void GoToPage(WindowDataHandle theData, short mainTopic, Boolean updateNow);
static void GetTextResources(short mainTopic);
static void DisposeTextResources(void);
static void CalculateXRefInfo(short index, short *mainTopic, Str255 name);
short HelpWindowDispatch(WindowDataHandle theData, short theMessage, unsigned long misc)
{
unsigned char theChar;
Point thePoint;
short theDepth;
switch (theMessage)
{
case kUpdate:
theDepth=misc&0x7fff;
DrawTheHelpWindow(theDepth);
return kSuccess;
break;
case kKeydown:
theChar=misc&charCodeMask;
KeyPressedInHelpWindow(theData, theChar);
return kSuccess;
break;
case kMousedown:
thePoint.h=(misc>>16)&0x7fff;
thePoint.v=misc&0x7fff;
MouseClickedInHelpWindow(theData, thePoint);
return kSuccess;
break;
case kOpen:
OpenTheHelpWindow(theData);
return kSuccess;
break;
case kInitialize:
InitializeTheHelpWindow(theData);
return kSuccess;
break;
case kStartup:
SetupTheHelpWindow(theData);
return kSuccess;
break;
case kShutdown:
ShutdownTheHelpWindow(theData);
return kSuccess;
break;
}
return kFailure; /* revert to default processing for all other messages */
}
void SetupTheHelpWindow(WindowDataHandle theData)
{
short i,j;
unsigned char *helpStr="\pHelp";
Handle temp;
short textID;
short centeringOffset;
temp=GetResource('STR#', MAIN_TOPIC_ID);
gNumMainTopics=**((short**)temp);
ReleaseResource(temp);
centeringOffset=DEAD_SPACE_TOP;
for (i=0; i<gNumMainTopics; i++)
{
GetIndString(gMainTopicTitle[i], MAIN_TOPIC_ID, i+1);
gMainTopicID[i]=ParseRawTitle(gMainTopicTitle[i], gXRefIndex[i], &(gNumXRefs[i]));
SetRect(&gMainTopicRect[i], DEAD_SPACE_LEFT, centeringOffset+(BUTTON_HEIGHT+BUTTON_GAP)*i,
DEAD_SPACE_LEFT+BUTTON_WIDTH, centeringOffset+(BUTTON_HEIGHT+BUTTON_GAP)*(i+1));
}
for (i=0; i<MAX_XREFS; i++)
{
SetRect(&gXRefRect[i], XREF_TEXT_WIDTH+DEAD_SPACE_LEFT+BUTTON_WIDTH+DEAD_SPACE_LEFT+
i*(XREF_WIDTH+XREF_GAP), DEAD_SPACE_TOP+TEXT_RECT_HEIGHT+XREF_DEAD_SPACE_TOP,
XREF_TEXT_WIDTH+DEAD_SPACE_LEFT+BUTTON_WIDTH+DEAD_SPACE_LEFT+
i*(XREF_WIDTH+XREF_GAP)+XREF_WIDTH, DEAD_SPACE_TOP+TEXT_RECT_HEIGHT+
XREF_DEAD_SPACE_TOP+XREF_HEIGHT);
}
gTheText=gTheStyle=0L;
GoToPage(0L, gMainTopicShowing, FALSE);
SetRect(&gTextRect, DEAD_SPACE_LEFT+BUTTON_WIDTH+DEAD_SPACE_LEFT, DEAD_SPACE_TOP,
DEAD_SPACE_LEFT+BUTTON_WIDTH+DEAD_SPACE_LEFT+TEXT_RECT_WIDTH,
DEAD_SPACE_TOP+TEXT_RECT_HEIGHT);
(**theData).maxDepth=8;
(**theData).windowWidth=DEAD_SPACE_LEFT+BUTTON_WIDTH+DEAD_SPACE_LEFT+
TEXT_RECT_WIDTH+DEAD_SPACE_RIGHT;
(**theData).windowHeight=DEAD_SPACE_TOP+TEXT_RECT_HEIGHT+DEAD_SPACE_BOTTOM;
(**theData).windowType=noGrowDocProc; /* document-looking thing */
(**theData).hasCloseBox=TRUE;
(**theData).windowBounds.top=50;
(**theData).windowBounds.left=6;
SetIndWindowTitle(kHelp, helpStr);
}
void ShutdownTheHelpWindow(WindowDataHandle theData)
{
DisposeTextResources();
}
void InitializeTheHelpWindow(WindowDataHandle theData)
{
(**theData).initialTopLeft.v=(**theData).windowBounds.top-9;
(**theData).initialTopLeft.h=(**theData).windowBounds.left;
}
void OpenTheHelpWindow(WindowDataHandle theData)
{
(**theData).offscreenNeedsUpdate=TRUE;
}
void KeyPressedInHelpWindow(WindowDataHandle theData, unsigned char keyPressed)
{
short oldTopic;
ObscureCursor();
switch (keyPressed)
{
case 0x1c: /* left arrow */
gMainTopicShowing--;
if (gMainTopicShowing<0)
gMainTopicShowing=gNumMainTopics-1;
GoToPage(theData, gMainTopicShowing, TRUE);
break;
case 0x1d: /* right arrow */
gMainTopicShowing++;
if (gMainTopicShowing>=gNumMainTopics)
gMainTopicShowing=0;
GoToPage(theData, gMainTopicShowing, TRUE);
break;
case 0x1b: /* escape key */
CloseTheWindow(theData);
break;
}
}
void MouseClickedInHelpWindow(WindowDataHandle theData, Point mouseLoc)
{
short theDepth;
short i;
Str255 name;
short newMain;
theDepth=(**theData).windowDepth;
for (i=0; i<gNumXRefs[gMainTopicShowing]; i++)
{
if (PtInRect(mouseLoc, &gXRefRect[i]))
{
CalculateXRefInfo(gXRefIndex[gMainTopicShowing][i], &newMain, name);
if (newMain!=-1)
{
if (Track3DButton(&gXRefRect[i], name, 0L, theDepth))
{
GoToPage(theData, newMain, TRUE);
return;
}
}
}
}
for (i=0; i<gNumMainTopics; i++)
{
if (PtInRect(mouseLoc, &gMainTopicRect[i]))
{
if (Track3DButton(&gMainTopicRect[i], gMainTopicTitle[i], 0L, theDepth))
{
GoToPage(theData, i, TRUE);
return;
}
}
}
}
void DrawTheHelpWindow(short theDepth)
{
GrafPtr curPort;
short i,j;
Boolean isColor;
Rect tempRect;
Str255 theStr;
short dummy1, dummy2;
isColor=(theDepth>2);
GetPort(&curPort);
EraseRect(&(curPort->portRect));
DrawTheShadowBox(gTextRect);
if (gTheText!=0L)
{
tempRect=gTextRect;
InsetRect(&tempRect, 8, 4);
DrawTheText(gTheText, gTheStyle, kLeft, srcOr, tempRect);
}
for (i=0; i<gNumMainTopics; i++)
{
Draw3DButton(&gMainTopicRect[i], gMainTopicTitle[i], 0L, theDepth, FALSE);
}
for (i=0; i<gNumXRefs[gMainTopicShowing]; i++)
{
CalculateXRefInfo(gXRefIndex[gMainTopicShowing][i], &dummy1, theStr);
Draw3DButton(&gXRefRect[i], theStr, 0L, theDepth, FALSE);
}
if (gNumXRefs[gMainTopicShowing]>0)
{
MoveTo(gXRefRect[0].left-XREF_TEXT_WIDTH, gXRefRect[0].bottom-5);
TextFont(geneva);
TextSize(9);
DrawString("\pSee also:");
}
}
void DrawTheText(CharHandle theText, StylHandle theStyleHandle, short theJust,
short theMode, Rect theRect)
{
short i, numStyles;
long textPos;
long maxOffset;
Str255 thisLine;
Boolean notDoneYet;
unsigned char thisChar;
short theRow, theCol;
unsigned char lastEnd, thisEnd;
Boolean overRun;
numStyles=(**theStyleHandle).numStyles;
textPos=0L;
theRow=theRect.top+(**theStyleHandle).theStyle[0].fontDescent+1;
theCol=theRect.left;
thisLine[0]=0x00;
lastEnd=0;
for (i=0; i<numStyles; i++)
{
if (i==numStyles-1)
maxOffset=GetHandleSize(theText);
else
maxOffset=(**theStyleHandle).theStyle[i+1].offset;
TextFont((**theStyleHandle).theStyle[i].fontNum);
TextFace((**theStyleHandle).theStyle[i].fontStyle);
TextSize((**theStyleHandle).theStyle[i].fontSize);
TextMode(theMode);
while (textPos<maxOffset)
{
notDoneYet=TRUE;
while ((textPos<maxOffset) && (notDoneYet))
{
thisChar=thisLine[++thisLine[0]]=(*theText)[textPos++];
notDoneYet=((thisChar!=' ') && (thisChar!=0x0d));
}
thisEnd=thisLine[0];
overRun=(theRect.right-theCol<=StringWidth(thisLine));
if ((overRun) || (thisChar==0x0d) || (textPos==maxOffset))
{
if (overRun)
thisLine[0]=lastEnd;
if (theJust==kCenter)
MoveTo((theRect.right-theRect.left-StringWidth(thisLine))/2+
theCol, theRow);
else
MoveTo(theCol, theRow);
theCol+=StringWidth(thisLine);
DrawString(thisLine);
if (overRun)
{
BlockMove(&thisLine[lastEnd+1], &thisLine[1], thisEnd-lastEnd+1);
if (thisEnd>=lastEnd)
{
thisLine[0]=thisEnd-lastEnd-1;
textPos--;
}
else
thisEnd=thisLine[0]=0x00;
}
else thisLine[0]=0x00;
if ((overRun) || (thisChar==0x0d))
{
theRow+=(**theStyleHandle).theStyle[i].lineHeight;
theCol=theRect.left;
}
}
lastEnd=thisEnd;
}
if (thisLine[0]!=0x00)
{
if (theJust==kCenter)
MoveTo((theRect.right-theRect.left-StringWidth(thisLine))/2+
theCol, theRow);
else
MoveTo(theCol, theRow);
theCol+=StringWidth(thisLine);
DrawString(thisLine);
thisLine[0]=0x00;
}
}
TextMode(srcOr);
}
void DrawTheShadowBox(Rect theRect)
{
theRect.right-=2;
theRect.bottom-=2;
FrameRect(&theRect);
MoveTo(theRect.left+3, theRect.bottom+1);
Line(theRect.right-theRect.left-2, 0);
Line(0, -theRect.bottom+theRect.top+3);
MoveTo(theRect.left+3, theRect.bottom);
Line(theRect.right-theRect.left-3, 0);
Line(0, -theRect.bottom+theRect.top+4);
}
short ParseRawTitle(Str255 theTitle, short *xRef, short *numXRefs)
{
Str255 numStr;
unsigned long result;
short i,j;
Boolean gotbullet;
Boolean moreXRefs;
if (xRef!=0L)
{
*numXRefs=0;
for (j=1, gotbullet=FALSE; ((j<=theTitle[0]) && (!gotbullet)); j++)
gotbullet=(theTitle[j]=='%');
if (gotbullet)
{
i=j;
do
{
numStr[0]=0x00;
while ((numStr[0]<=theTitle[0]-i) &&
(((numStr[numStr[0]]=theTitle[i+(numStr[0]++)]))!=' ')) {}
if (numStr[numStr[0]]==' ')
{
moreXRefs=TRUE;
i+=numStr[0];
numStr[0]--;
}
else moreXRefs=FALSE;
StringToNum(numStr, &result);
xRef[(*numXRefs)++]=result;
}
while (moreXRefs);
theTitle[0]=j-2;
}
}
numStr[0]=0x00;
while ((numStr[numStr[0]]=theTitle[++numStr[0]])!=' ') {}
theTitle[0]-=numStr[0];
Mymemcpy(&theTitle[1], &theTitle[numStr[0]+1], theTitle[0]);
numStr[0]--;
StringToNum(numStr, &result);
return result;
}
void GoToPage(WindowDataHandle theData, short mainTopic, Boolean updateNow)
{
DisposeTextResources();
GetTextResources(mainTopic);
gMainTopicShowing=mainTopic;
if (updateNow)
{
(**theData).offscreenNeedsUpdate=TRUE;
UpdateTheWindow(theData);
}
}
void GetTextResources(short mainTopic)
{
short resID;
DisposeTextResources();
resID=gMainTopicID[mainTopic];
gTheText=GetResource('TEXT', resID);
gTheStyle=GetResource('styl', resID);
}
void DisposeTextResources(void)
{
if (gTheText!=0L)
ReleaseResource(gTheText);
if (gTheStyle!=0L)
ReleaseResource(gTheStyle);
gTheText=gTheStyle=0L;
}
void CalculateXRefInfo(short index, short *mainTopic, Str255 name)
{
short theMain;
unsigned char *bad="\pBad XRef!";
for (theMain=0; theMain<gNumMainTopics; theMain++)
{
if (index==gMainTopicID[theMain])
{
Mymemcpy(name, gMainTopicTitle[theMain], gMainTopicTitle[theMain][0]+1);
*mainTopic=theMain;
return;
}
}
Mymemcpy(name, bad, bad[0]+1);
*mainTopic=-1;
}